package com.jxb.lession76.web.servlet;

import com.jxb.lession76.common.Constant;
import com.jxb.lession76.entity.Emp;
import com.jxb.lession76.services.EmpServices;
import com.jxb.lession76.services.impl.EmpServicesImpl;
import com.jxb.lession76.util.StringUtil;
import jdk.nashorn.internal.ir.IfNode;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/emp")
public class EmpController extends BaseController {
    private EmpServices es = new EmpServicesImpl();

    public String deleteEmp(HttpServletRequest req,HttpServletResponse resp){
        int eno = StringUtil.StringToInt(req.getParameter("eno"));
        boolean result = es.deleteEmp(eno);
        if (result == true) {
            return Constant.Re.REDIRECT + "/emp?method=getAll";
        }
        return Constant.Re.REDIRECT + "/message.jsp";
    }

    public String getAll(HttpServletRequest req,HttpServletResponse resp){
        List<Emp> empList = es.getAll();
        if (empList != null) {
            req.setAttribute("empList",empList);
            return Constant.Re.FORWARD + "/empList.jsp";
        }
        return Constant.Re.REDIRECT + "/index.jsp";
    }
}
